package popcorn.ticket.controller;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import popcorn.ticket.entity.Users;
import popcorn.ticket.service.OssService;
import popcorn.ticket.service.UsersService;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.MD5Util;
import popcorn.ticket.tool.PageData;

@Controller
public class UsersController {
	@Autowired
	UsersService usersService;
	@Autowired
	private OssService os;
	
	Logger logger = Logger.getLogger(UsersController.class);
	// 分页
	@GetMapping(value = "userspage", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Users> listBlogByPage1(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "keywords", defaultValue = "") String keywords) {
		
		//判断用户选择筛选功能是选择管理员还是用户
		if ("1".equals(keywords)||"0".equals(keywords)) {
			PageData<Users> pd = usersService.selectUsersByPower(page, limit, keywords);
			// 直接返回分页的数据
			return pd;
		}
		PageData<Users> pd = usersService.selectUsersByLike(page, limit, keywords);
		// 直接返回分页的数据
		return pd;
	}

	// 修改
	@PostMapping(value = "updUsers", consumes = "application/json", produces = "application/json;charset=utf-8")
	@ResponseBody
	// 参数位置获取前台传递的json值
		public AjaxResponse updUsers(@RequestBody Users users) {
			String msg = "修改失败";
			if (users != null) {
				int flag = usersService.updateByPrimaryKeySelective(users);
				if (flag>0) {
					msg = "修改成功";
				}
			}
			AjaxResponse ajaxResponse = new AjaxResponse(msg, 200);
			return ajaxResponse;
	}
	// 增加
	@PostMapping(value = "addUsers", consumes = "application/json", produces = "application/json;charset=utf-8")
	@ResponseBody
	// 参数位置获取前台传递的json值
	public AjaxResponse addUsers(@RequestBody Users users) {
		
		String msg = "增加失败";
		if (users != null) {
			int flag = usersService.addUsers(users);
			if (flag>0) {
				msg = "增加成功";
			}
		}
		AjaxResponse aj = new AjaxResponse(msg, 200);
		return aj;
	}
	// 删除
	@PostMapping(value = "delUsers/{userid}",produces = "application/json;charset=utf-8")
	// 参数位置获取前台传递的json值
	@ResponseBody
	public AjaxResponse delUsers(@PathVariable("userid") int userid) {
		String msg = "删除失败";

		if (userid > 0) {
			int flag = usersService.delUsersById(userid);
			if (flag>0) {
				msg = "删除成功";
			}
		}
		AjaxResponse aj = new AjaxResponse(msg, 200);
		return aj;
	}
	// 重置照片
	@PostMapping(value = "resetpic/{userid}",produces = "application/json;charset=utf-8")
	// 参数位置获取前台传递的json值
	@ResponseBody
	public AjaxResponse resetpic(@PathVariable("userid") int userid) {
		Users users = new Users();
		String msg = "重置失败";
		//判断是否输入一个空置
		if (userid > 0) {
			users.setUserid(userid);
			users.setUserpic("./images/header.jpg");
			int flag = usersService.updateByPrimaryKeySelective(users);
			if (flag>0) {
				msg = "重置成功";
			}
		}
		AjaxResponse aj = new AjaxResponse(msg, 200);
		return aj;
	}
	// 重置密码
	@PostMapping(value = "resetpwd/{userid}",produces = "application/json;charset=utf-8")
	// 参数位置获取前台传递的json值
	@ResponseBody
	public AjaxResponse resetpwd(@PathVariable("userid") int userid) {
		Users users = new Users();
		String msg = "重置失败";
		//判断是否输入一个空置
		if (userid > 0) {
			users.setUserid(userid);
			String pwd = "666666";
			users.setUserpwd(pwd);
			int flag = usersService.updateByPrimaryKeySelective(users);
			if (flag>0) {
				msg = "重置成功";
			}
		}
		AjaxResponse aj = new AjaxResponse(msg, 200);
		return aj;
	}
	
	// 获取单个用户全部信息
	@RequestMapping("getUser/{userid}")
	@ResponseBody
	public Users getUser(@PathVariable("userid") int userid) {

		return usersService.getUser(userid);
	}
	
	// 判断用户要修改密码时，输入的原密码是否一致
	@RequestMapping("judgePwd")
	@ResponseBody
	public boolean judgeOldpwd(String oldpwd,int userid) {
		logger.info("old:"+oldpwd);
		String pwd = usersService.getUser(userid).getUserpwd();
		logger.info("pwd:"+pwd);
		if(pwd.equals(MD5Util.getEncodeByMd5(oldpwd))) {
			return true;
		}
		
		return false;
	}
	
	// 修改用户头像
	@RequestMapping("upduserimg")
	@ResponseBody
	public AjaxResponse updUserimg(Users user,MultipartFile uploadPic) {
		
		AjaxResponse ar = new AjaxResponse("修改失败", 0);
		logger.debug("123:"+user);
		String url = os.uploadUserImg(uploadPic, user.getUserid()+"");
		user.setUserpic(url);
		int n = usersService.updateByPrimaryKeySelective(user);
		if(n > 0) {
			ar.setCode(200);
			ar.setMsg("修改成功");
		}
		return ar;
	}

	
}
